import store from '@/store';
/**
 * 转换时间格式
 * 用法：format(ms,'yyyy-MM-dd hh:mm:ss')
 * @param time 毫秒数
 * @param fmt 要转换的时间格式
 */
export function format(time, fmt) {
  if (!time) {
    return ''
  }
  var d = new Date(time)
  var o = {
    'M+': d.getMonth() + 1, //月份
    'd+': d.getDate(), //日
    'h+': d.getHours(), //小时
    'm+': d.getMinutes(), //分
    's+': d.getSeconds(), //秒
    'q+': Math.floor((d.getMonth() + 3) / 3), //季度
    S: d.getMilliseconds() //毫秒
  }
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(
      RegExp.$1,
      (d.getFullYear() + '').substr(4 - RegExp.$1.length)
    )
  for (var k in o)
    if (new RegExp('(' + k + ')').test(fmt))
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
      )
  return fmt
}

export function UUID() {
  var d = new Date().getTime()
  if (window.performance && typeof window.performance.now === 'function') {
    d += performance.now() //use high-precision timer if available
  }
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(
    c
  ) {
    var r = (d + Math.random() * 16) % 16 | 0
    d = Math.floor(d / 16)
    return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16)
  })
  return uuid
}

export function PrefixInteger(num, n) {
  return (Array(n).join(0) + num).slice(-n)
}

/**
 * 获取客户端类型 [APP:1, 网页:2, 微信:3]
 */
export function getClientId() {
  const agent = navigator.userAgent
  if (agent.includes('Html5Plus')) return 1
  else if (agent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger')
    return 3
  else return 2
}

/**
 * 获取客户端类型 [APP:APP, 网页:WAP, 微信:WX]
 */
export function getClient() {
  const agent = navigator.userAgent
  if (agent.includes('Html5Plus')) return "APP"
  else if (agent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger')
    return "WX"
  else return "WAP"
}
/**
 * wx页面授权方法
 */
export function wxAuth() {
  window.isWxAuthFlag = true
  const appid = 'wx455d4595caac679c'
  const redirect_uri =  encodeURIComponent(`${process.env.VUE_APP_HOST}wx.html?returnUrl=${location.href}`) 
  window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&connect_redirect=1#wechat_redirect`
}
